//
//  HWContextCell.m
//  嗨文
//
//  Created by 响当当 on 5/15/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWContextCell.h"

@interface HWContextCell()
@property (nonatomic, strong)UILabel *titleLabel;//标题
@property (nonatomic, strong)UILabel *contextLabel;//文章
@end

@implementation HWContextCell

- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(10*kUIScaleBasePlus,20*kUIScaleBasePlus,SCREEN_WIDTH-20*kUIScaleBasePlus,30*kUIScaleBasePlus)
                                         textAligment:NSTextAlignmentLeft
                                             textClor:LCHexColor(0x545454)
                                             textFont:25*kUIScaleBasePlus];
    }
    return _titleLabel;
}

- (UILabel *)contextLabel
{
    if (!_contextLabel)
    {
        _contextLabel = [HWUtilties creatLabelWithFrame:CGRectMake(10*kUIScaleBasePlus,CGRectGetMaxY(self.titleLabel.frame)+5*kUIScaleBasePlus,SCREEN_WIDTH-20*kUIScaleBasePlus,0)
                                         textAligment:NSTextAlignmentLeft
                                             textClor:LCHexColor(0x545454)
                                             textFont:18*kUIScaleBasePlus];
        _contextLabel.numberOfLines = 0;
    }
    return _contextLabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contextLabel];
    }
    return self;
}

- (void)getContext:(HWBookReaderModel *)model
{
    self.titleLabel.text = model.chapterTitle;
    [self.titleLabel sizeToFit];
    
    NSString *str = model.chapterContent;
    self.contextLabel.text = [HWUtilties adjustParagraphFormat:str];
    [self.contextLabel sizeToFit];
}

#pragma mark - 计算cell高度
/** 获取cell高度 */
- (CGFloat)heightWithModel:(HWBookReaderModel *)model fontSize:(UIFont *)font
{
    NSString *str = model.chapterContent;
    NSString *string = [HWUtilties adjustParagraphFormat:str];
    
    NSArray *arr = [string componentsSeparatedByString:@"\n"];
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:string];
    
    NSRange allRange = [string rangeOfString:string];
    
    [attrStr addAttribute:NSFontAttributeName value:font range:allRange];
    
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor]range:allRange];
    
    CGFloat titleHeight;
    
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    // 获取label的最大宽度
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, CGFLOAT_MAX)options:options context:nil];
    
    titleHeight = ceilf(rect.size.height);
    
    return titleHeight*kUIScaleBasePlus + 5*arr.count*kUIScaleBasePlus; // 动态高度 + 静态高度
}
- (void)changeTextFont:(NSInteger)font context:(HWBookReaderModel *)model
{
    NSString *str = model.chapterContent;
    
    NSArray *arr = [HWUtilties pagingWithContentString:str
                                           contentSize:kContentSize
                                         textAttribute:nil];
    
    self.contextLabel.text = [HWUtilties adjustParagraphFormat:str];
    self.contextLabel.font = [UIFont systemFontOfSize:font*kUIScaleBasePlus];
    [self.contextLabel sizeToFit];

    
    
}

- (void)changeContextColor:(UIColor *)color
{
    self.contextLabel.textColor = color;
    self.titleLabel.textColor = color;
}

@end
